<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Easy Send</title>
     // testing
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link href="style.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript" src="js/jquery.js"></script>
		<script type="text/javascript" src="js/cufon-yui.js"></script>
		<script type="text/javascript" src="js/arial.js"></script>
		<script type="text/javascript" src="js/cuf_run.js"></script>
		<script type="text/javascript" src="js/radius.js"></script>
		<script type="text/javascript">
			function colorize(item, color){
					var n=item.childNodes.length;
					for(var i=0; i<n; i++){
						if(item.childNodes[i].nodeName=='TD'){
							item.childNodes[i].style.backgroundColor=color;
							if(item.childNodes[i].childNodes.length>0){
								if(item.childNodes[i].childNodes[0].nodeName=='A'){
									item.childNodes[i].childNodes[0].style.backgroundColor=color;
								}
							}
						}
					}
				}
				
			function validate_form()
			{
					var f=document.forms["myForm"]["fname"].value
					if (f==null || f=="")
					{
						alert("First name must be filled out");
						document.forms["myForm"]["fname"].focus();
						return false;
					}
					  
					var l=document.forms["myForm"]["lname"].value
					if (l==null || l=="")
					{
						alert("Last name must be filled out");
						document.forms["myForm"]["lname"].focus();
						return false;
					}
					
					var n=document.forms["myForm"]["number"].value
					if (n==null || n=="")
					{
						alert("Mobile number must be filled out");
						document.forms["myForm"]["number"].focus();
						return false;
					}
					
					if (n!=parseInt(n))
					{
						alert("Mobile number must be integers (e.g. 15101234567)");
						document.forms["myForm"]["number"].focus();
						return false;
					}
					
			}
			
				
		</script>
	</head>

	<body>
		
		
		

		<div class="main">
			<div class="header">
				<div class="header_resize">
					<div class="logo"> <img src="images/logo.jpg" alt="" />
						<h1><a href="index.html"><span>Easy</span>Send</a><small>Simple SMS System</small></h1>
					</div>
					
					<div class="clr"></div>
					
					<div class="menu_nav">
							<ul>
								<li class="active"><a href="index.html">Home</a></li>
								<li><a href="about.html">About Us</a></li>
								<li class="last"><a href="contact.html">Contact Us</a></li>
							</ul>
							<div class="search">
								<form id="form" name="form" method="post" action="#">
									<span>
									<input name="q" type="text" class="keywords" id="textfield" maxlength="50" value="Search..." />
									<input name="b" type="image" src="images/search.gif" class="button" />
									</span>
								</form>
							</div>
					</div>
					<div class="clr"></div>
				</div>
			</div>
			
			<div class="clr"></div>
			<div class="content">
				<div class="content_resize">
					<div class="mainbar">
						<div class="article">
							<h2><span>Contacts</span></h2>
							<div class="clr"></div>
														
							
								
							<?php
								$DBserver = "mysql4.000webhost.com";
								$DBuser = "a5265253_admin";
								$DBpass = "cs3520";
								$DBname = "a5265253_sms";

								// Setting up connection to the DB server
								$con = mysql_connect($DBserver,$DBuser,$DBpass);
								
								if (!$con)
								{
									die("Could not connect: ".mysql_error());
								}
								
								
								mysql_select_db($DBname,$con);
								
								$result = mysql_query("SELECT * FROM contacts ORDER BY FirstName, LastName");
								$count = 1;
								echo "<form action='contacts.php' method='POST'>";
								//echo '<div style="overflow:auto; height:356px; width:400px" >';
								echo "<table>
												<tr>
													<th></th>
													<th>First Name</th>
													<th>Last Name</th>
													<th>Phone</th>
													<th>Group</th>
												</tr>";
								while ($row = mysql_fetch_array($result))
								{
									if ($count%2 == 0)
									{
										echo '<tr class="alt">';
									}
									
									else
									{
										echo '<tr>';
									}
									
									echo "<td><input type='checkbox' name='records[]' value=".$row['Phone']." /></td>";
									echo "<td>".$row['FirstName']."</td>";
									echo "<td>".$row['LastName']."</td>";
									echo "<td>".$row['Phone']."</td>";
									echo "<td>".$row['Grp']."</td>";
									echo "</tr>";
									
									$count++;
								}
								
								echo "</table><br /><br />";
								//echo "</div><br />";
								
								echo "<input type='submit' name='submit' value='Delete' onClick='return confirm(\"Are you sure you want to delete all selected record\(s\)\")' />
									  </form>";
									  
									  
								echo '<br /><br /><hr /><br />- To add a new contact, fill the following fields then click on "Add":<br /><br />
									
									<form name="myForm" action="contacts.php" method="POST">
									<table class="form">
										<tr class="form">
											<td class="form">First name:</td> 
											<td class="form"><input type="text" name="fname" /></td>
										</tr>
										<tr class="form">
											<td class="form">Last name:</td>
											<td class="form"><input type="text" name="lname" /></td>
										</tr>
										<tr class="form">
											<td class="form">Mobile number:</td>
											<td class="form"><input type="text" name="number" /></td>
										</tr>
										<tr class="form">
											<td class="form">Group:</td>
											<td class="form"><input type="text" name="group" /> (optional)</td>
										</tr>
									</table>
									<br />
									<input type="submit" name="submit" value="Add" onClick="return validate_form()"/></form>';
									
								
								
								
								if ( isset($_POST['submit']) ) 
								{
									switch ($_POST['submit'])
									{
										case 'Add':
										{
											$FirstName = $_POST['fname'];
											$LastName = $_POST['lname'];
											$Phone = $_POST['number'];
											$Group = $_POST['group'];
											
											if ($Group == "")
											{
												$cmd = "INSERT INTO contacts (FirstName, LastName, Phone) VALUES ('".$FirstName."', '".$LastName."', '".$Phone."')";
											}
											else
											{
												$cmd = "INSERT INTO contacts (FirstName, LastName, Phone, Grp) VALUES ('".$FirstName."', '".$LastName."', '".$Phone."', '".$Group."')";
											}
											
											mysql_query($cmd) or die ("Error inserting into database".mysql_error()); 
											
											break;
										}
				
										case 'Delete':
										{
											$selected = $_POST['records'];
											
											while (count($selected) > 0)
											{
												$Phone = array_shift($selected);
												
												$cmd = "DELETE FROM contacts WHERE Phone='".$Phone."'";
												mysql_query($cmd) or die ("Error deleting from database".mysql_error()); 
											}	
											
											break;
												
										}
										
										default:	
				
									}
																		
									mysql_close($con);
									echo "<script type='text/javascript'>location.href = 'contacts.php'</script>";
								}
								
								
							?>
								
					
							
							
						</div>
		
					</div>
	  
				<div class="sidebar">
					<div class="gadget">
						<h2 class="star">Messages</h2>
						<div class="clr"></div>
						<ul class="sb_menu">
							<li><a href="newMsg.php">New message</a></li>
							<li><a href="inbox.php">Inbox</a></li>
							<li><a href="sentMsg.php">Sent messages</a></li>
							<li><a href="contacts.php">Contacts</a></li>
						</ul>
					</div>
       
				</div>
				<div class="clr"></div>
			</div>
			</div>
			<div class="fbg">

				<div class="footer">
					<p class="lf">Copyright &copy; 2011 <a href="index.html">EasySend</a> - All Rights Reserved</p>
      
					<div class="clr"></div>
				</div>
			</div>
		</div>
		
		
		<?php
			
		?>

		
	</body>
</html>
